package gl.android.view.action;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import at.markushi.ui.R;

public class MainActivity extends Activity implements OnClickListener {

	private ActionView actionView;
	int i = 0;
	private RevealColorView revealColorView;
	private boolean isExpand;
	private int backgroundColor;
	private View rl;
	private View image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		actionView = (ActionView) findViewById(R.id.action);
		actionView.setAnimationDuration(1200);
		actionView.setOnClickListener(this);
		rl = findViewById(R.id.rl);
		image = findViewById(R.id.imageView1);
		findViewById(R.id.button1).setOnClickListener(this);

		findViewById(R.id.button1).setTag("#185b51");
		actionView.setTag("#185b51");
		revealColorView = (RevealColorView) findViewById(R.id.reveal);
		backgroundColor = Color.parseColor("#212121");

	}

	private Point getLocationInView(View src, View target) {
		final int[] l0 = new int[2];
		src.getLocationOnScreen(l0);

		final int[] l1 = new int[2];
		target.getLocationOnScreen(l1);

		l1[0] = l1[0] - l0[0] + target.getWidth() / 2;
		l1[1] = l1[1] - l0[1] + target.getHeight() / 2;

		return new Point(l1[0], l1[1]);
	}

	private int getColor(View view) {
		return Color.parseColor((String) view.getTag());
	}

	int old;
	private int old_w;
	private int old_h;

	@Override
	public void onClick(View v) {
		final int color = getColor(v);
		final Point p = getLocationInView(revealColorView, v);
		if (old == 0) {
			old = rl.getHeight();
		}
		if (old_w == 0) {
			old_w = image.getWidth();
		}
		if (old_h == 0) {
			old_h = image.getHeight();
		}
		if (!(isExpand=!isExpand)) {
			revealColorView.hide(p.x, p.y, backgroundColor, 0, 1200, null);

			ValueAnimator va = ValueAnimator.ofInt(1920, old);

			va.setDuration(700);
			va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				public void onAnimationUpdate(ValueAnimator animation) {
					Integer value = (Integer) animation.getAnimatedValue();
					rl.getLayoutParams().height = value.intValue();
					rl.requestLayout();
					image.getLayoutParams().height = value.intValue();
					image.requestLayout();
				}
			});
			va.start();
			
			ValueAnimator va_image_w = ValueAnimator.ofInt(1920, old_w);

			va_image_w.setDuration(900);
			va_image_w.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				public void onAnimationUpdate(ValueAnimator animation) {
					Integer value = (Integer) animation.getAnimatedValue();
					image.getLayoutParams().width = value.intValue();
					image.requestLayout();
				}
			});
			va_image_w.start();
			
			ValueAnimator va_image_h = ValueAnimator.ofInt(1920, old_h);
			va_image_h.setDuration(900);
			va_image_h.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				public void onAnimationUpdate(ValueAnimator animation) {
					Integer value = (Integer) animation.getAnimatedValue();
					image.getLayoutParams().height = value.intValue();
					image.requestLayout();
				}
			});
			rl.setPadding(0, 0, 0, 0);
			va_image_h.start();

		} else {
			revealColorView.reveal(p.x, p.y, color, v.getHeight() / 2, 1400,
					null);

			ValueAnimator va = ValueAnimator.ofInt(old, 1920);

			va.setDuration(700);
			va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				public void onAnimationUpdate(ValueAnimator animation) {
					Integer value = (Integer) animation.getAnimatedValue();
					rl.getLayoutParams().height = value.intValue();
					rl.requestLayout();
					image.getLayoutParams().height = value.intValue();
					image.requestLayout();
				}
			});
			rl.setPadding(0, 0, 0, 0);
			va.start();
			
			ValueAnimator va_image_w = ValueAnimator.ofInt(old_w,1920 );

			va_image_w.setDuration(600);
			va_image_w.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				public void onAnimationUpdate(ValueAnimator animation) {
					Integer value = (Integer) animation.getAnimatedValue();
					image.getLayoutParams().width = value.intValue();
					image.requestLayout();
				}
			});
			va_image_w.start();
			
			ValueAnimator va_image_h = ValueAnimator.ofInt(old_h,1920);
			va_image_h.setDuration(600);
			va_image_h.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
				public void onAnimationUpdate(ValueAnimator animation) {
					Integer value = (Integer) animation.getAnimatedValue();
					image.getLayoutParams().height = value.intValue();
					image.requestLayout();
				}
			});
			va_image_h.start();
		}

		i++;
		switch (i) {
		case 0:
			actionView.setAction(new BackAction(),
					ActionView.ROTATE_COUNTER_CLOCKWISE);
			break;
		case 1:
			actionView.setAction(new CloseAction(),
					ActionView.ROTATE_COUNTER_CLOCKWISE);
			break;
		case 2:
			actionView.setAction(new DrawerAction(),
					ActionView.ROTATE_COUNTER_CLOCKWISE);
			break;
		case 3:
			actionView.setAction(new PlusAction(),
					ActionView.ROTATE_COUNTER_CLOCKWISE);
			break;

		default:
			i = 0;
			actionView.setAction(new BackAction(), ActionView.ROTATE_CLOCKWISE);
			break;
		}

	}

}
